//var headurl = `https://www.jihemu.com/bookapi/`
var headurl = `https://techcherry.cn/gundamapi/`
//`https://bwv.net.cn/${__wxConfig.envVersion == 'trial' ? 'prod' :'api'}`;


 

var imgurl = `${headurl}imgs/`, audiourl = `${headurl}audio/`  
class api {

  getheadurl() {
    return headurl;
  }

  getimgurl() {
    return imgurl;
  }
  getaudiourl() {
    return audiourl;
  }
  /**
   * 登录
   * code- string, 小程序调用login生成的code(临时登录凭证)
   */
  wxlogin(code, success, fail) {
    fstpostdata('/login/wxlogin', {
      jscode: code
    }, res => success(res), res => fail&&fail(res))
  }

  

  
  
  uploadimg(data, params) {
    // console.log(data,params,"q11fesfds");
    // return;

    var that = this,
      i = data.i ? data.i : 0, //当前上传的哪张图片
      success = data.success ? data.success : 0, //上传成功的个数
      fail = data.fail ? data.fail : 0, //上传失败的个数
      pics = data.pics ? data.pics : [];
    // console.log(data.path[i]);
    wx.uploadFile({
      header: {
        'content-type': "application/x-www-form-urlencoded",
        token: getApp().logininfo.Token,
        openid: getApp().logininfo.openid,
        userid: getApp().logininfo.userid
      },
      url: data.url,
      filePath: data.path[i],
      name: 'upload', //这里根据自己的实际情况改
      formData: null, //这里是上传图片时一起上传的数据
      success: (resp) => {
        success++; //图片上传成功，图片上传成功的变量+1
        let result = JSON.parse(resp.data);
        pics.push(result.path);
        console.log(pics, "picssss")
        //这里可能有BUG，失败也会执行这里,所以这里应该是后台返回过来的状态码为成功时，这里的success才+1 
      },
      fail: (res) => {
        fail++; //图片上传失败，图片上传失败的变量+1
        console.log('fail:' + i + "fail:" + fail);
      },
      complete: () => {
        console.log(i);

        i++; //这个图片执行完上传后，开始上传下一张            
        if (i == data.path.length) { //当图片传完时，停止调用          
          console.log('执行完毕');
          console.log('成功：' + success + " 失败：" + fail);

          params.success(pics);

        } else { //若图片还没有传完，则继续调用函数                
          console.log(i);
          data.i = i;
          data.success = success;
          data.fail = fail;
          data.pics = pics;
          that.uploadimg(data, params);
        }
      }
    });
  }
  // 收藏音频详情
  // audio/collfindaudiobyid
  // id
 
  // 设置上门取件时间
  // order/bk_setbackdate

  // 获取商品列表
  getgoodslist(data, succ) {
      postdata('/Goods/ft_getgoodslist',data, res => succ(res))
  }
  // 获取商品详情
  getgoodsdetailbyid(id, succ) {
      postdata('/Goods/findbyids',{id}, res => succ(res))
  }
  //获取商品商品购物车列表
  getcargoodslist(succ) {
      postdata('/cart/getgoodslist',{}, res => succ(res))
  }
  // 设置商品购买数量
  setCartcount(data, succ) {
      postdata('/Cart/addpaynum',data, res => succ(res))
  }

  // 小程序获取分类列表
  getGoodsCate( succ) {
        postdata('/GoodsCate/cate_mp_getlist',{}, res => succ(res))
    }


  // 获取商品数据
getGoodsCateList( succ) {
      postdata('/Main/getgoodsmain',{}, res => succ(res))
  }


  // 小程序获取商品订单列表
getShopOrderlist(data, succ) {
      postdata('/Order/minip_getgoodslist',data, res => succ(res))
  }
  // 前端根据cid获取商品分页列表
  getmainpicpath(data, succ) {
        postdata('/Goods/ft_getgoodslistbycid',data, res => succ(res))
    }

 
  setbackdate(data, succ) {
      postdata('/order/bk_setbackdate',data, res => succ(res))
  }
  // 获取我的申请详情

  identity(succ) {
      postdata('/UserApply/findbyid',{}, res => succ(res))
  }

  // 添加合伙人申请
  addidentity(phone,succ) {
      postdata('/UserApply/add',{phone}, res => succ(res))
  }
  // 记录登录昵称和头像
  getnameurl(data,succ) {
      postdata('/Users/minip_getnameurl',data, res => succ(res))
  }
  accountlist(succ) {
      postdata('/Users/minip_accountlist',{}, res => succ(res))
  }
  minip_accountlist
  //收藏音频详情
  collfindaudiobyid(id, succ) {
      postdata('/audio/collfindaudiobyid',{id}, res => succ(res))
  }
    //物流信息订阅
  orderTracesSubByJson(data, succ) {
      postdata('/order/orderTracesSubByJson', data, res => succ(res))
    }
  // 小程序获取排序1-4的列表
  cate_mp_getlistbyorder(succ) {
        postdata('/audio/cate_mp_getlistbyorder', {}, res => succ(res))
      }
  // 单号识别
  orderdistinguish(lcode, succ) {
    postdata('/settings/orderdistinguish', {lcode}, res => succ(res))
  }
  // 即时查询
  traceorder(data, succ) {
    postdata('/settings/traceorder', data, res => succ(res))
  }
  // 获取全部轮播
  // getallswiper(succ) {
  //   postdata('Swiper/getallswiper', {}, res => succ(res))
  // }


   // 获取全部轮播
   getallswiper(type,succ) {
    postdata('/swiper/getallswiperbytype', type, res => succ(res))
  }
  // 获取根据id获取详情

getswiperdetail(id,succ) {
  postdata('Swiper/findbyids', {id}, res => succ(res))
}
  //修改用户为vip
  setvip(data, succ) {
    postdata('users/setvip', {}, res => succ(res))
  }

  //获取绘本阅读
  // audio/getlistbyid
  // pagesize
  // start
  getresadlist(data, succ) {
    postdata('audio/getlistbyid', data, res => succ(res))
  }

  //发送推送
  ft_sendts(data, succ) {
    postdata('Template/ft_sendts', data, res => succ(res))
  }
  
//发送推送
ft_sendts1(data, succ) {
  postdata('Template/ft_sendts1', data, res => succ(res))
}

  //获取打卡排行榜
  ft_getdaysrank(data, succ) {
    postdata('Comments/ft_getdaysrank', data, res => succ(res))
  }

  //获取音频分类
  audiocatempgetlist( succ) {
    postdata('audio/cate_mp_getlist',{}, res => succ(res))
  }
  gatherformid(e, succ) {
    if (e.detail.formId.indexOf('formId') > -1) return;
    postdata('home/addformid', { formid: e.detail.formId }, res => succ(res))
  }
  //分页获取音频
  audiogetlist(data, succ) {
    postdata('audio/getlist', data, res => succ(res))
  }
  //音频详情页
  audiofindaudiobyid(id, succ) {
    postdata('audio/findaudiobyid', {id}, res => succ(res))
  }
  // 删除音频评论
  commentsaudel(id, succ) {
    postdata('comments/au_del', { id }, res => succ(res))
  }

 

  
  // 获取列表
  commentsaugetlist(data, succ) {
    postdata('comments/au_getlist', data, res => succ(res))
  }

  // 添加评论
  commentsauadd(data, succ) {
    postdata('comments/au_add', data, res => succ(res))
  }

  // 增加浏览数
  audioaddview(id, succ) {
    postdata('audio/addview', { id }, res => succ(res))
  }
  // 收藏或取消
  collectionaod(aid, succ) {
    postdata('collection/aod', { aid }, res => succ(res))
  }
  
  // 获取收藏列表
  collectionmpgetlist(data, succ) {
    postdata('collection/mp_getlist', data, res => succ(res))
  }

  // 搜索
  audiosearch(keyword, succ) {
    postdata('audio/ft_search', { keyword }, res => succ(res))
  }
  // 在分类里搜索
  searchinclass(data, succ){
  postdata('audio/searchinclass', data, res => succ(res))
}
  // 音频评论点赞
  commentsaufavor(data, succ) {
    postdata('comments/au_favor', data, res => succ(res))
  }

  //通过id寻找班级
  findclassbyid(id, succ) {
    postdata('class/findbyid', {
      id
    }, res => succ(res))
  }
//绑定班级
  binduserclass(cid,succ){
    postdata('users/bindclass', {
      cid
    }, res => succ(res))
  }
  // 音频点赞
  audiofavor(audioid, succ) {
    postdata('audio/favor', { audioid }, res => succ(res))
  }
  

  //添加评论回复
  addcomment(data, succ) { //data {contents,username,avatarurl}
    postdata('comments/resp_add', data, res => succ(res))
  }

  //获取评论列表
  resp_mp_getlist(data, succ) {
    postdata('comments/resp_mp_getlist', data, res => succ(res))
  }

  getawards(data, succ) {
    postdata('awards/ft_getlist', data, res => succ(res))
  }
  getmasters(data, succ) {
    postdata('master/ft_getlist', data, res => succ(res))
  }
  addcart(data, succ) {
    postdata('cart/add', data, res => succ(res))
  }


  addmorecart(data, succ) {
    postdata('cart/addmore', data, res => succ(res))
  }
  getaddressbyid(data, succ) {
    postdata('address/getaddressbyid', data, res => succ(res))
  }
  getorderlist(data, succ) {
    postdata('order/minip_getlist', data, res => succ(res))
  }
  delcart(data, succ) {
    postdata('cart/minip_del', data, res => succ&&succ(res))
  }
  getcarts(succ) {
    postdata('cart/getlist', {}, res => succ(res))
  }
  getbookbyage(data, succ) {
    postdata('goods/getbookbyage', data, res => succ(res))
  }

  findbookbyid(id, succ) {
    postdata('goods/findbyid', {id}, res => succ(res))
  }

  mp_getminestate( type,succ) {
    postdata('order/mp_getminestate', { type}, res => succ(res))
  }

  getdepositlist(data,succ) {
    postdata('deposit/mp_getlist', data, res => succ(res))
  }
  


  cancelorder(data, succ) {
    postdata('order/cancel', data, res => succ(res))
  }
  pay(data, succ) {
    postdata('order/pay', data, res => succ(res))
  }
  afterpaysucc(data, succ) {
    postdata('order/afterpaysucc', data, res => succ(res))
  }
  addorder(data, succ) {
    postdata('order/addOrder', data, res => succ(res))
  }
  getaward(id, succ) {
    postdata('awards/getawardbyid', id, res => succ(res))
  }
  getmaster(id, succ) {
    postdata('master/getmasterbyid', id, res => succ(res))
  }

  getmore(data, succ) {
    postdata('main/getmore', data, res => succ(res))
  }
  getbooks(ids, succ) {
    postdata('goods/findbooksbyids', ids, res => succ(res))
  }
  getsetting(succ) {
    postdata('home/getsettings', {}, res => succ(res))
  }
  getaddressbyid(data, succ) {
    postdata('address/getaddressbyid', data, res => succ(res))
  }
  deladdress(data, succ) {
    postdata('address/del', data, res => succ(res))
  }
  setaddresscheck(data, succ) {
    postdata('address/setcheck', data, res => succ(res))
  }

  addressaoe(data, succ) {
    postdata('address/aoe', data, res => succ(res))
  }
  getaddresslist(succ) {
    postdata('address/getlist', {}, res => succ(res))
  }

  addcomments(data, succ) {
    postdata('Comments/add', data, res => succ(res))
  }
  updateuser(data, succ) {
    postdata('users/updateUser', data, res => succ(res))
  }


  getcommentlist(data, succ) {
    postdata('comments/mp_getlistbycondition', data, res => succ(res))
  }

  // getmycommentlist(data, succ) {
  //   postdata('Comments/mp_getmylist', data, res => succ(res))
  // }
  //   getcommentlist(data,succ) {
  //     postdata('Comments/mp_getlist',data, res => succ(res))
  //   }
  delcomment(id, succ) {
    postdata('Comments/mp_del', {
      id
    }, res => succ(res))
  }
  dofavor(id, succ) {
    postdata('Comments/mp_favor', {
      id
    }, res => succ(res))
  }

  getfavorlist(succ) {
    postdata('users/getfavorlist', {}, res => succ(res))
  }
  check(uid, succ) {
    postdata('login/wxcheck', {
      userid: uid
    }, res => succ(res))
  }
  getlistbycid(cid, succ) {
    postdata('goods/getgoodsbycid', {
      cid
    }, res => succ(res))
  }
  getcategory(succ) {
    postdata('category/getlist', {}, res => succ(res))
  }
  getmain(pagesize, succ) {
    postdata('main/getmain', {
      pagesize
    }, res => succ(res))
  }

  getgoods(ids, succ) {
    postdata('goods/getlist', {
      ids
    }, res => succ(res))
  }

  getscoregoods(ids, succ) {
    postdata('/goods/getsscoregoodsblist', {
      ids
    }, res => succ(res))
  }

  getcoupons(succ) {
    postdata('/goods/getcoupons', {}, res => succ(res))
  }

  getcouponbyid(id, succ) {
    postdata('/goods/getcouponbyid', {
      id
    }, res => succ(res))
  }

  rewardcoupon(ids, succ) {
    postdata('/goods/rewardCoupon', {
      ids
    }, res => succ(res))
  }

  search(words, succ) {
    postdata('/goods/getlistbywords', {
      words
    }, res => succ(res))
  }
  searchgoods(data, succ) {
    postdata('goods/bk_searchlist', data, res => succ(res))
  }
  // changekucun(data,succ) {
  //   postdata('/goods/changekucun', data, res => succ(res)) 
  // }

  cartaoe(count, goodsid, index, id, succ) {
    postdata('/cart/aoe', {
      count,
      goodsid,
      id
    }, res => succ(res))
  }

  getcart(succ) {
    postdata('/cart/getlist', {}, res => succ(res))
  }
  addorder(data, succ) {
    postdata('/order/addOrder', data, res => succ(res))
  }

  afterpay(data, couponid, ordernumber, realamount, succ) {
    postdata('/order/afterpaysucc', {
      data,
      couponid,
      ordernumber,
      realamount
    }, res => succ(res))
  }

  order_changestate(data, succ) {
    postdata('/order/changestate', data, res => succ(res))
  }

  order_getlist(data, succ) {
    postdata('/order/getlist', data, res => succ(res))
  }

  requestrefund(ordernumber, succ) {
    postdata('/order/requestrefund', {
      ordernumber
    }, res => succ(res))
  }
  getUserAvailableCoupon(amount, succ) {
    postdata('/goods/getUserAvailableCoupon', {
      amount
    }, res => succ(res))
  }
  requestrefund(ordernumber, succ) {
    postdata('/order/requestrefund', {
      ordernumber
    }, res => succ(res))
  }

  order_list(state, succ) {
    postdata('/order/getorderlist', {
      state
    }, res => succ(res))
  }
  calculateamount(amount, couponid, succ) {
    postdata('/goods/calculateamount', {
      amount,
      couponid
    }, res => succ(res))
  }
  getscore(succ) {
    postdata('/users/getscore', {}, res => succ(res))
  }

  share_getlist(data,succ) {
    postdata('/DZ_ProjectDetail/minip_getlist1', data, res => succ(res))
  }

  daka_getlist(data,succ) {
    postdata('/comments/minip_getlist', data, res => succ(res))
  }

  share_del(id, succ) {
    postdata('DZ_ProjectDetail/minip_del', { id }, res => succ(res))
  }

  share_add(data,succ) {
    postdata('/DZ_ProjectDetail/minip_add', data, res => succ(res))
  }

  share_zhuanru(data,succ) {
    postdata('/DZ_room/minip_changepool', data, res => succ(res)) 
  } 

  修改到期时间(data,succ){
    postdata('/users/minip_editdeadline',data, res => succ(res)) 
  }  

  删除用户(id,succ){
    postdata('/users/minip_deluser',{id}, res => succ(res)) 
  } 
  


  完工(id,succ) {
    postdata('/DZ_ProjectDetail/minip_finished', {id}, res => succ(res)) 
  }  

  share_genzong(data,succ) {
    postdata('/DZ_ProjectDetail/minip_setnote', data, res => succ(res))
  }

  share_upordown(data,succ) {
    postdata('/DZ_ProjectDetail/minip_upordown', data, res => succ(res))
  }

  share_del(id,succ) {
    postdata('/DZ_ProjectDetail/minip_del', {id}, res => succ(res))
  }

  share_setnote(id,note,succ) {
    postdata('/DZ_ProjectDetail/minip_del', {id,note}, res => succ(res))
  }

  share_setaddress(data,succ) {
    postdata('/DZ_ProjectDetail/minip_setaddress', data, res => succ(res))
  }

  daka_add(data,succ) {
    postdata('/comments/minip_add',data, res => succ(res))
  }

  dwg_add(data,succ) {
    postdata('/comments/minip_adddwg',data, res => succ(res))
  }

  dwg_add(data,succ) {
    postdata('/comments/minip_adddwg',data, res => succ(res))
  }

  share_settop(id,succ) {
    postdata('/DZ_ProjectDetail/minip_settop', {id}, res => succ(res))
  }

  share_getd(id,succ) {
    postdata('/DZ_ProjectDetail/minip_getdetail', {id}, res => succ(res))
  }

  report_add(data,succ) {
    let path = data.reportid>0?'/comments/minip_reportadd':'/DZ_report/minip_add'
    postdata(path, data, res => succ(res))
  }

  report_getall(succ) {
    postdata('/DZ_report/minip_getlist', {}, res => succ(res))
  }

  report_del(id,succ) {
    postdata('/DZ_report/minip_del', {id}, res => succ(res))
  }

  report_addcomment(data,succ) {
    postdata('/comments/minip_reportadd', data, res => succ(res))
  }

  getphone(data,succ){
    postdata('/users/pdlgete', data, res => succ(res))
  }
 

bindphone(phone,succ){
  postdata('/users/pdlbindphone', {phone}, res => succ(res))
}


getuseradress(data,succ){
  postdata('/users/getuseradress', data, res => succ(res))
}


setuseradress(succ){
  postdata('/users/setuseradress', {}, res => succ(res))
}
// 用户支付了育儿分享
// users/setnine
setnine(succ){
  postdata('/users/setnine', {}, res => succ(res))
}
  /**
   * 关注/粉丝
   */
  mine_getfollowandfans(succ) {
    postdata('/users/mine_getfollowandfans', {}, res => succ(res))
  }

  follow(data,succ) {
    postdata('/users/follow', data, res => succ(res))
  }

  myfof(data, succ) {
    postdata('/users/myfof', data, res => succ(res))
  }

  getusermainpage(data, succ) {
    postdata('/users/getusermainpage', data, res => succ(res))
  }

//   获取关注列表
// UserFoucs/getlist
// pagesize
// start
getfoucslist(succ) {
  postdata('/Users/getfollowlist', {}, res => succ(res))
}

// 关注
// UserFoucs/setfollow
// focusid//被关注的用户id
setfollow(fanid, succ) {
  postdata('/Users/setfollow', {fanid}, res => succ(res))
}

// 取关
// UserFoucs/takeoff
// focusid//被关注的用户id
// takeoff(focusid, succ) {
//   postdata('/UserFoucs/takeoff', {focusid}, res => succ(res))
// }

// liu
//普通用户绑定代理人id
checkshare(agentid, succ) {
  postdata('/Users/getuseragentid', {agentid}, res => succ(res))
}
//前端获取所有的科目
Coursetypegetlist( succ) {
  postdata('/Coursetype/ft_getlist', {}, res => succ(res))
}
//前端获取所有的试卷
Testquestionsgetlist(data, succ) {
  postdata('/Testquestions/ft_getlist', data, res => succ(res))
}
//前端获取试卷的题目
Subjectgetlist(cid, succ) {
  postdata('/Subject/ft_getlist', {cid}, res => succ(res))
}

// end liu

// 修改或者新建题目
aoeEvaluating(data, succ) {
  postdata('/Evaluating/aoe',data, res => succ(res))
}


getEvaluatingDetail(id,succ) {
  postdata('/Evaluating/getdetail',{id}, res => succ(res))
}
// 4.获取所有的作品集
// getEvaluatingList(data,succ) {
//   postdata('/Evaluating/getalllist',data, res => succ(res))
// }
getEvaluatingList(data,succ) {
  postdata('/Works/getlist',data, res => succ(res))
}
// 2.获取用户这个试卷所有的答题
getAllsampleReels(data,succ) {
  postdata('/Evaluating/getlist',data, res => succ(res))
}
// 7.点赞或取消点赞
setlike(id,succ) {
  postdata('/Works/setlike',{id}, res => succ(res))
}
// 6.新建作品评论
aoeWorkscomment(data,succ) {
  postdata('/Workscomment/aoe',data, res => succ(res))
}
// 8. 前端获取这个作品所有评论
getWorkscomment(id,succ) {
  postdata('/Workscomment/ft_getalllist',{id}, res => succ(res))
}

// 3.获取用户自己的作品集
getMyWorkscomment(data,succ) {
  postdata('/Works/getlistbyuserid',data, res => succ(res))
}
// 8. 修改或者新建幼儿园
aoeKindergarten(data,succ) {
  postdata('/Kindergarten/aoe',data, res => succ(res))
}
// 9. 幼儿园详情
getKindergarten(succ) {
  postdata('/Kindergarten/getdetail',{}, res => succ(res))
}



//支付9.9
parentingpay(data, succ, fail) {
  data['openid'] = wx.getStorageSync('openid')
  postdata('/order/parentingpay', data, res => {
    var data = JSON.parse(res.data.res);
    var orderid = data.orderid;
    wx.requestPayment({
      timeStamp: data.timeStamp,
      nonceStr: data.nonce_str,
      package: data.package,
      paySign: data.paySign,
      signType: 'MD5',
      success: res => {
        console.log(res, 'succ')
        if (res.errMsg.indexOf('ok') > -1) {
          succ && succ({ succ: 1, orderid})
        }
      },
      fail: res => {
        //支付取消
        succ && succ({succ:-1})
      }
    })
  })
}







  prepay(data, succ, fail) {
    data['openid'] = wx.getStorageSync('openid')
    postdata('/order/pay', data, res => {
      var data = JSON.parse(res.data.res);
      var orderid = data.orderid;
      wx.requestPayment({
        timeStamp: data.timeStamp,
        nonceStr: data.nonce_str,
        package: data.package,
        paySign: data.paySign,
        signType: 'MD5',
        success: res => {
          console.log(res, 'succ')
          if (res.errMsg.indexOf('ok') > -1) {
            succ && succ({ succ: 1, orderid})
          }
        },
        fail: res => {
          //支付取消
          succ && succ({succ:-1})
        }
      })
    })
  }
}

function postdata(urlparms, data, success, fail) {
 
  
  wx.request({
    url: headurl + urlparms,
    method: "POST",
    data: data,
    header: {
      'content-type': "application/x-www-form-urlencoded",
      openid: getApp().logininfo.openid,
      userid: getApp().logininfo.userid
    },
    success: res => success(res),
    fail: res => fail && fail(res)
  })
}

function getdata(urlparms, data, success, fail) {

  wx.request({
    url: headurl + urlparms,
    data: data,
    header: {
      'content-type': "application/x-www-form-urlencoded",
      'userId': getApp().userinfo.userId,
    },
    success: res => success(res),
    fail: res => fail && fail(res)
  })
}

function fstpostdata(urlparms, data, success, fail) {
  wx.request({
    url: headurl + urlparms,
    method: "POST",
    data: data,
    header: {
      'content-type': "application/x-www-form-urlencoded",
    },
    success: res => success(res),
    fail: res => fail && fail(res)
  })
}



module.exports = api